package com.blq.web.ServeClassify.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.blq.common.utils.StringUtils;
import com.blq.common.core.page.TableDataInfo;
import com.blq.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.blq.web.ServeClassify.domain.bo.ServeClassifyBo;
import com.blq.web.ServeClassify.domain.vo.ServeClassifyVo;
import com.blq.web.ServeClassify.domain.ServeClassify;
import com.blq.web.ServeClassify.mapper.ServeClassifyMapper;
import com.blq.web.ServeClassify.service.IServeClassifyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 服务类别Service业务层处理
 *
 * @author Blq
 * @date 2022-11-07
 */
@RequiredArgsConstructor
@Service
public class ServeClassifyServiceImpl extends ServiceImpl<ServeClassifyMapper,ServeClassify> implements IServeClassifyService {

    private final ServeClassifyMapper baseMapper;

    /**
     * 查询服务类别
     */
    @Override
    public ServeClassifyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务类别列表
     */
    @Override
    public TableDataInfo<ServeClassifyVo> queryPageList(ServeClassifyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServeClassify> lqw = buildQueryWrapper(bo);
        Page<ServeClassifyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务类别列表
     */
    @Override
    public List<ServeClassifyVo> queryList(ServeClassifyBo bo) {
        LambdaQueryWrapper<ServeClassify> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServeClassify> buildQueryWrapper(ServeClassifyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServeClassify> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ServeClassify::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增服务类别
     */
    @Override
    public Boolean insertByBo(ServeClassifyBo bo) {
        ServeClassify add = BeanUtil.toBean(bo, ServeClassify.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务类别
     */
    @Override
    public Boolean updateByBo(ServeClassifyBo bo) {
        ServeClassify update = BeanUtil.toBean(bo, ServeClassify.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ServeClassify entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除服务类别
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
